package com.daffodil.workable.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.daffodil.common.controller.ReactiveBaseController;
import com.daffodil.core.annotation.DictConver;
import com.daffodil.core.entity.JsonResult;
import com.daffodil.core.entity.Page;
import com.daffodil.core.entity.TableResult;
import com.daffodil.framework.annotation.AuthPermission;
import com.daffodil.framework.annotation.Log;
import com.daffodil.framework.annotation.Log.BusinessType;
import com.daffodil.util.StringUtils;
import com.daffodil.workable.constant.WorkableConstant;
import com.daffodil.workable.entity.WorkableForm;
import com.daffodil.workable.service.IWorkableFormService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;
import reactor.core.publisher.Mono;

/**
 * -节点表单控制层
 * @author yweijian
 * @date 2022-04-07
 * @version 1.0
 * @description
 */
@Api(value = "节点表单管理", tags = "节点表单管理")
@RestController
@RequestMapping(WorkableConstant.API_CONTENT_PATH + "/form")
public class WorkableFormController extends ReactiveBaseController {

    @Autowired
    private IWorkableFormService workableFormService;

    @ApiOperation("分页查询节点表单列表")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @SuppressWarnings("unchecked")
    @DictConver
    @AuthPermission("workable:form:list")
    @GetMapping("/list")
    public Mono<TableResult> list(WorkableForm workableForm, Page page, @ApiIgnore ServerHttpRequest request) {
        initQuery(workableForm, page, request);
        List<WorkableForm> list = workableFormService.selectWorkableFormList(query);
        return Mono.just(TableResult.success(list, query));
    }

    @ApiOperation("获取节点表单详情")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @GetMapping("/info")
    public Mono<JsonResult> info(@ApiParam(value = "节点表单ID") String id, 
            @ApiParam(value = "计划任务ID") String taskId, 
            @ApiParam(value = "节点编号ID") String nodeId, 
            @ApiIgnore ServerHttpRequest request) {
        
        if(StringUtils.isNotEmpty(id)) {
            WorkableForm workableForm = workableFormService.selectWorkableFormById(id);
            return Mono.just(JsonResult.success(workableForm));
        }
        
        WorkableForm workableForm = workableFormService.selectWorkableFormByTaskIdAndNodeId(taskId, nodeId);
        return Mono.just(JsonResult.success(workableForm));
    }

    @ApiOperation("新增节点表单")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:form:add")
    @Log(title = "节点表单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Mono<JsonResult> add(@Validated @RequestBody WorkableForm workableForm, @ApiIgnore ServerHttpRequest request) {
        workableFormService.insertWorkableForm(workableForm);
        return Mono.just(JsonResult.success(workableForm));
    }

    @ApiOperation("修改节点表单")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:form:edit")
    @Log(title = "节点表单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Mono<JsonResult> edit(@Validated @RequestBody WorkableForm workableForm, @ApiIgnore ServerHttpRequest request) {
        workableFormService.updateWorkableForm(workableForm);
        return Mono.just(JsonResult.success(workableForm));
    }

    @ApiOperation("删除节点表单")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:form:remove")
    @Log(title = "节点表单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Mono<JsonResult> remove(@RequestBody String[] ids, @ApiIgnore ServerHttpRequest request) {
        workableFormService.deleteWorkableFormByIds(ids);
        return Mono.just(JsonResult.success());
    }

}
